package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author wy
 * @Date 2021/2/26 17:21
 * @Version 1.0
 */
public class Consumer {

    public static void main(String[] args) throws Exception {
        //1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        //后面与服务器进行通信 都是用这个通信通道来进行
        Channel c = con.createChannel();

        //2.创建helloworld队列
        c.queueDeclare("helloworld",true,false,false,null);


        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] a = delivery.getBody();
                String msg = new String(a);
                System.out.println("收到啊傻逼"+msg);

            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

        //3.接受消息 收到的信息数据 会交给一个回调对象进行处理
        /**
         * 第二个参数
         *          true:是否自动确认
         */
        c.basicConsume("helloworld",true,deliverCallback,cancelCallback);
    }


}
